/**
 * 获取图片完成加载后的实例对象
 */
export const getImage = (imageData: any) => {
  return new Promise((resolve, reject) => {
    let img = new Image()
    img.src = imageData
    img.crossOrigin = 'anonymous'
    img.onload = function () {
      resolve(img)
    }
  })
}
interface IPoint {
  x: number
  y: number
}
/**
 * 已知三点坐标求角的度数
 */
export function getAngle(A: IPoint, B: IPoint, C: IPoint) {
  // 通过坐标算边长的平方
  let a2 = Math.pow(C.x - B.x, 2) + Math.pow(C.y - B.y, 2)
  let b2 = Math.pow(A.x - C.x, 2) + Math.pow(A.y - C.y, 2)
  let c2 = Math.pow(B.x - A.x, 2) + Math.pow(B.y - A.y, 2)

  let a = Math.sqrt(a2)
  let b = Math.sqrt(b2)
  let c = Math.sqrt(c2)

  // 通过边长算余弦
  let cosA = Math.acos((b2 + c2 - a2) / (b * c * 2))
  let cosB = Math.acos((a2 + c2 - b2) / (a * c * 2))
  let cosC = Math.acos((a2 + b2 - c2) / (a * b * 2))
  // 余弦和角度的比值
  let cosThan = Math.PI / 180
  // 通过余弦算角度
  return {
    A: cosA / cosThan,
    B: cosB / cosThan,
    C: cosC / cosThan,
  }
}
/**
 * 已知两条直角边的长度 求斜边的长度
 */
export function findHypotenuse(base: number, perpendicular: number) {
  const bSquare = base ** 2
  const pSquare = perpendicular ** 2
  const sum = bSquare + pSquare
  const hypotenuse = Math.sqrt(sum)
  return hypotenuse
}
